#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org
START=30
USE_PROCD=1

ssmtp() {
	local section="$1"
	
	config_get "mx"	      "$section" "mx"
	config_get "sender"   "$section" "sender"
	config_get "user"     "$section" "user"
	config_get "pwd"      "$section" "pwd"
	config_get "authtype" "$section" "authtype"
	config_get_bool "TLS" "$section" "TLS"

	mkdir -p /var/etc
	rm /var/etc/revaliases 2>/dev/null

	echo  >/var/etc/ssmtp.conf "mailhub=$mx"

	[ -n "$sender" ] && {
		echo  >/var/etc/revaliases "root:$sender"
		echo >>/var/etc/ssmtp.conf "root=$sender"
	}

	[ -n "$user" ]   && echo >>/var/etc/ssmtp.conf "AuthUser=$user"
	[ -n "$pwd" ]    && echo >>/var/etc/ssmtp.conf "AuthPass=$pwd"
	[ -n $authtype ] && echo >>/var/etc/ssmtp.conf "AuthMethod=\"$authtype\""
	[ "$TLS" -ne 0 ] && {
		echo >>/var/etc/ssmtp.conf "UseTLS=yes"
		echo >>/var/etc/ssmtp.conf "UseStartTLS=yes"
	}
}

reload_service() {
	config_load ssmtp
	config_foreach ssmtp ssmtp
}

service_triggers() {
	procd_add_reload_trigger "ssmtp"
}

start_service() {
	reload_service
}
